<?php
if (!defined("__WD__")) exit("Wrong Connect");

class Kong_Proc extends __Kong{
	
	function __construct() {

		parent::__construct();

		$this->load->module( "mysql" , $this->db );
		$this->load->module( "input" , $this->input );

	}
	function doit() {
		
		// Action!
		if ($this->input->post_exists("action")) {
			switch( $this->input->post("action") ) {
				case "new_board" :
					$this->newBoard();break;
				case "modify_board" :
					$this->modifyBoard();break;
				case "delete_board" :
					$this->deleteBoard();break;
				case "new_category_board" :
					$this->newCategoryBoard();break;
				case "modify_category_board" :
					$this->modifyCategoryBoard();break;
				case "grant_board" :
					$this->grantBoard();break;
					
				case "modify_article" :
					$this->modifyArticle();break;
				case "delete_article" :
					$this->deleteArticle();break;

				case "modify_comment" :
					$this->modifyComment();break;
				case "delete_comment" :
					$this->deleteComment();break;
					
			}
		}
	}
	
	function newBoard() {
		
		$id			= $this->input->post("id");
		$name		= $this->input->post("name");
		
		$dat = array(
			'id'		=> $id,
			'name'		=> $name
		);
		
		if ( $this->db->count("select count(*) from `wd_board` where `id`=?", array($id)) > 0 ) alert("같은 ID가 존재합니다.");
		if ( $this->db->insert("wd_board", $dat) === false ) alert("DB를 쓰는 도중 실패하였습니다.");

		go($this->data->href."/".$this->data->kong->id."/board");
	}
	
	function modifyBoard() {

		$idx	= $this->input->post("idx");
		$id		= $this->input->post("id");
		$name	= $this->input->post("name");
		
		$html	= $this->input->post("html");
		$options = array( 'html' => $html );

		$dat = array(
			'id'		=> $id,
			'name'		=> $name,
			'options'	=> serialize($options)
		);

		if ( $this->db->count("select count(*) from `wd_board` where `id`=? and `idx`<>?", array($id, $idx)) > 0 ) alert("같은 ID가 존재합니다.");
		if ( $this->db->update("wd_board", $dat, array("idx" => $idx)) === false ) alert("DB를 쓰는 도중 실패하였습니다.");
/*
			$rst_group = $this->db->fetch_assoc("select `group_idx` from `wd_category_member` where `member_id`=?", array($id));
			foreach($rst_group as $k => $v) {$rst_group[$k] = $v['group_idx'];}

			for($i = 0; $i < count($groups); $i++) {
				if ( !in_array($groups[$i], $rst_group) ) {
					$dat = array(
						'group_idx'		=> $groups[$i],
						'member_id'		=> $id
					);
					if ( $this->db->insert("wd_category_member", $dat) === false ) alert("DB를 쓰는 도중 실패하였습니다.");
				}
			}
			for($i = 0; $i < count($rst_group); $i++) {
				if ( !in_array($rst_group[$i], $groups) ) {
					$dat = array(
						'group_idx'		=> $rst_group[$i],
						'member_id'		=> $id
					);
					if ( $this->db->delete("wd_category_member", $dat) === false ) alert("DB를 쓰는 도중 실패하였습니다.");
				}
			}
*/
		
		go($this->data->href."/".$this->data->kong->id."/board");

	}
	function newCategoryBoard() {
		
		
		$idx		= $this->input->post("idx");
		$id			= $this->input->post("id");
		$name		= $this->input->post("name");
		
		$order		= $this->db->count("select count(*) from `wd_category` where `board_id`=?", array($id));
		
		$dat = array(
			'board_id'	=> $id,
			'name'		=> $name,
			'order'		=> $order
		);

		if ( $this->db->insert("wd_category", $dat) === false ) alert("DB를 쓰는 도중 실패하였습니다.");

		go($this->data->href."/".$this->data->kong->id."/board/".$id."/category");
	}
	function modifyCategoryBoard() {
		
		$idx	= $this->input->post("idx");
		$id		= $this->input->post("id");

		$category_name	= $this->input->post("category_name");
		$category_idx	= $this->input->post("category_idx");

		$rows	= $this->db->fetch_assoc_one("select `idx` from `wd_category` where `board_id`=?", array($id) );

		for ($i = 0; $i < count($rows); $i++) {
			if ( in_array($rows[$i], $category_idx) ) { // Modify
				$order = array_search($rows[$i], $category_idx);
				$dat = array(
					'name'		=> $category_name[$order],
					'order'		=> $order
				);
				if ( $this->db->update("wd_category", $dat, array("idx" => $rows[$i])) === false ) alert("DB를 쓰는 도중 실패하였습니다.");
			}
			else { // Delete
				if ( $this->db->delete("wd_category", array("idx" => $rows[$i])) === false ) alert("DB를 쓰는 도중 실패하였습니다.");
			}
		}
		
		go($this->data->href."/".$this->data->kong->id."/board/".$id."/category");
	}
	function grantBoard() {

		$idx	 = $this->input->post("idx");
		
		$grant['list']		= $this->input->post("list", array());
		$grant['view']		= $this->input->post("view", array());
		$grant['write']		= $this->input->post("write", array());
		$grant['modify']	= $this->input->post("modify", array());
		$grant['delete']	= $this->input->post("delete", array());
		$grant['secret']	= $this->input->post("secret", array());
		
		$grant['commentwrite']	= $this->input->post("commentwrite", array());
		$grant['commentdelete']	= $this->input->post("commentdelete", array());
		
		$grant = serialize($grant);

		$dat = array(
			'grant'		=> $grant
		);

		if ( $this->db->update("wd_board", $dat, array("idx" => $idx)) === false ) alert("DB를 쓰는 도중 실패하였습니다.");
		
		go($this->data->href."/".$this->data->kong->id."/board");

	}

	function deleteBoard() {

		$idx = $this->input->post("idx");

		if ( $this->db->delete("wd_board", array("idx" => $idx)) === false ) alert("DB를 쓰는 도중 실패하였습니다.");
		
		go($this->data->href."/".$this->data->kong->id."/board");
		
	}
	
	
	function modifyArticle() {

		$idx			= $this->input->post("idx");

		$board_id		= $this->input->post("board_id");
		$category_idx	= $this->input->post("category_idx");
		$writer			= $this->input->post("writer");
		$subject		= $this->input->post("subject");
		$contents		= $this->input->post("contents");
		
		$dat = array(
			'board_id'		=> $board_id,
			'category_idx'	=> $category_idx,
			'writer'		=> $writer,
			'subject'		=> $subject,
			'contents'		=> $contents
		);
		
		if ( $this->db->update("wd_data", $dat, array("idx" => $idx)) === false ) alert("DB를 쓰는 도중 실패하였습니다.");

		go($this->data->href."/".$this->data->kong->id."/article");
		
	}
	function deleteArticle() {
		
		$idx = $this->input->post("idx");

		if ( $this->db->delete("wd_data", array("idx" => $idx)) === false ) alert("DB를 쓰는 도중 실패하였습니다.");
		
		go($this->data->href."/".$this->data->kong->id."/article");
		
	}
	
	function modifyComment() {

		$idx			= $this->input->post("idx");

		$writer			= $this->input->post("writer");
		$contents		= $this->input->post("contents");
		
		$dat = array(
			'writer'		=> $writer,
			'contents'		=> $contents
		);

		if ( $this->db->update("wd_comment", $dat, array("idx" => $idx)) === false ) alert("DB를 쓰는 도중 실패하였습니다.");

		go($this->data->href."/".$this->data->kong->id."/comment");
		
	}
	function deleteComment() {
		
		$idx = $this->input->post("idx");

		if ( $this->db->delete("wd_comment", array("idx" => $idx)) === false ) alert("DB를 쓰는 도중 실패하였습니다.");
		
		go($this->data->href."/".$this->data->kong->id."/comment");
		
	}
	
	
	// End
}